package com.dailyblue.java.jdbc.pack0325;

import java.sql.*;

public class DemoC {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        String url = "jdbc:mysql://localhost:3306/test";
        String user = "root";
        String password = "a123456";
        Connection conn = DriverManager.getConnection(url, user, password);
        String sql = "select * from emp where state=1";
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        // 递归迭代遍历
        // rs.next()指向下一行
        while (rs.next()) {
            // 目前 rs 代表当前行
            int empNo = rs.getInt(1);
            String ename = rs.getString(2);
            String job = rs.getString("job");
            Integer mgr = rs.getInt("mgr");
            String hireDate = rs.getString("hireDate");
            Double sal = rs.getDouble(6);
            Double comm = rs.getDouble(7);
            Integer deptNo = rs.getInt(8);
            System.out.print("empNo:" + empNo + ",");
            System.out.print("ename:" + ename + ",");
            System.out.print("job:" + job + ",");
            System.out.print("mgr:" + mgr + ",");
            System.out.print("hireDate:" + hireDate + ",");
            System.out.print("sal:" + sal + ",");
            System.out.print("comm:" + comm + ",");
            System.out.println("deptNo:" + deptNo);
        }
        rs.close();
        ps.close();
        conn.close();
    }
}
